import { type NextRequest, NextResponse } from "next/server"

// 配置为静态路由以支持静态导出
export const dynamic = 'force-static'

export async function GET(request: NextRequest) {
    try {
        // 验证token
        const token = request.headers.get("authorization")?.replace("Bearer ", "")
        if (!token) {
            return NextResponse.json({ success: false, message: "未授权" }, { status: 401 })
        }

        const { searchParams } = new URL(request.url)
        const startDate = searchParams.get("startDate")
        const endDate = searchParams.get("endDate")
        const format = searchParams.get("format")
        const status = searchParams.get("status")

        if (!startDate || !endDate || !format) {
            return NextResponse.json({ success: false, message: "缺少必要参数" }, { status: 400 })
        }

        // 这里应该调用真实的后端API
        // const response = await fetch(`http://localhost:8081/api/samples/export?${searchParams}`, {
        //   headers: { Authorization: `Bearer ${token}` }
        // })
        //
        // if (!response.ok) {
        //   throw new Error('导出失败')
        // }
        //
        // const blob = await response.blob()
        // const headers = new Headers()
        // headers.set('Content-Type', format === 'excel' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'application/pdf')
        // headers.set('Content-Disposition', `attachment; filename="export.${format === 'excel' ? 'xlsx' : 'pdf'}"`)
        //
        // return new NextResponse(blob, { headers })

        // 临时模拟响应，实际项目中应该删除
        const mockData = `样品处理完成记录导出\n时间范围: ${startDate} 至 ${endDate}\n格式: ${format}\n状态: ${status}`
        const blob = new Blob([mockData], { type: "text/plain" })

        const headers = new Headers()
        headers.set("Content-Type", "text/plain")
        headers.set("Content-Disposition", `attachment; filename="mock-export.txt"`)

        return new NextResponse(blob, { headers })
    } catch (error) {
        return NextResponse.json({ success: false, message: "导出失败" }, { status: 500 })
    }
}
